-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieEndpoint.java
More file actions
86 lines (78 loc) · 2.38 KB
/
MovieEndpoint.java
File metadata and controls
86 lines (78 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package at.htl.MovieManager.rest;
import at.htl.MovieManager.business.MovieDAO;
import at.htl.MovieManager.model.Movie;
import org.eclipse.microprofile.metrics.MetricUnits;
import org.eclipse.microprofile.metrics.annotation.Counted;
import org.eclipse.microprofile.metrics.annotation.Timed;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.net.URI;
@Path("movie")
@Produces(MediaType.APPLICATION_JSON)
public class MovieEndpoint {
@Inject
MovieDAO movieDAO;
//http://localhost:8080/movie?limit=1&offset=1
@GET
@Counted(name = "getAllMovies")
public Response getMovies(@QueryParam("offset")@DefaultValue("1")int offset, @QueryParam("limit")@DefaultValue("10")int limit){
return Response.ok(movieDAO.getAll(offset,limit)).build();
}
@GET
@Path("/{id}")
public Response getMoviesById(@PathParam("id") long id){
return Response.ok(movieDAO.getById(id)).build();
}
/*
{
"director":"Philipp Auinger",
"persons":[],
"release_date":2019,
"rt_score":100,
"title":"Das neue Abenteuer!"
}
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Transactional
public Response postMovie(Movie movie){
movieDAO.persist(movie);
return Response.created(URI.create("http://localhost:8080/api/product/" + movie.getId())).build();
}
@DELETE
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response deleteMovie(@PathParam("id") long id){
Movie entity = movieDAO.find(Movie.class, id);
if(entity != null){
entity = movieDAO.merge(entity);
movieDAO.remove(entity);
}
return Response.noContent().build();
}
/*
{
"director":"Philipp Auinger Jr.",
"id":21,
"persons":[],
"release_date":2019,
"rt_score":100,
"title":"Das neue neue Abenteuer!"
}
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public Response update(Movie movie){
movie = movieDAO.merge(movie);
//em.flush();
//em.refresh(product);
return Response.ok().entity(movie).build();
}
}